﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Cn.Edwon.Web.Support;
using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using System.IO;
using System.Drawing;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Client;
using Cn.Edwon.Web.Script;

namespace Cn.Cellula.Website.Ws {
    public partial class _Upload : PageSupport{
        IBiz biz = BizFactory.Get<IBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            try {
                UploadFile();
            } catch(Exception ex) {
                ActionFailed("上传时出现错误:"+ex.Message);
            }
        }
        private void UploadFile() {
            HttpPostedFile file = Request.Files[0];
            TUploadFileEO eo = new TUploadFileEO();
            ClientBiz.UploadFile(ref eo,file, Request.Form["model_name"], Request.Form["model_key"], this.OwnerKey, this.Actor);
            BizFactory.Get<IBiz>().Save<TUploadFileEO>(eo);
            ActionSucceed(eo);
        }
        private void ActionFailed(String msg) {
            msg = msg.Replace("\"", "\\\"");
            msg = msg.Replace("\n", "");
            msg = msg.Replace("\r", "");
            String str = "var Result = false;";
            str += "parent." + Request.Form["failed_handler"] + "(\"" + msg + "\")";
            Response.Clear();
            Response.Write("<script type=\"text/javascript\">"+str+"</script>");
        }
        private void ActionSucceed(TUploadFileEO eo) {
            String str = "parent." + Request.Form["succeed_handler"] + "(" + JsUtil.ParseEO2Json(eo) + ")";
            Response.Clear();
            Response.Write("<script type=\"text/javascript\">" + str + "</script>");
        }
    }
}
